package ltd.flyweight.gst200;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ResourceLoader;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@SpringBootApplication
public class Gst200Application implements ApplicationRunner {

    @Resource
    ResourceLoader resourceLoader;

    @Resource
    Gst200Parser gst200Parser;

    public static void main(String[] args) {
        SpringApplication.run(Gst200Application.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<String> lines = FileUtils.readLines(resourceLoader.getResource("classpath:journal.txt").getFile(), "GB2312");
        List<String> datum = new LinkedList<>();
        for (int i = 1; i < lines.size(); i++) {
            String line = lines.get(i);
            line = StringUtils.strip(line);
            String[] split = Arrays.stream(line.split(" {2}")).filter(s -> !s.isEmpty()).toArray(String[]::new);
            String data = split[3].trim() + "#" + StringUtils.joinWith("", split[7].trim().split(" "));
            datum.add(data);
        }
        datum.forEach(gst200Parser::parse);
    }
}
